package com.webapp.util;

import com.inforim.cognos.CognosClient;
import com.inforim.cognos.utils.CognosHelper;
import com.inforim.util.DESUtil;
import javax.security.auth.login.LoginException;
import org.apache.log4j.Logger;

public class GSKHelper
{
  static Logger logger = Logger.getLogger(GSKHelper.class);

  public static synchronized CognosClient getClient(String cognos_URL, String userName, String userPassword, String userNamespace) { CognosClient cognosClient = null;
    boolean authentication = true;
    if ((userName == null) || (userName.equals(""))) {
      authentication = false;
    } else {
      authentication = true;
      try {
        DESUtil desUtil = new DESUtil();
        userPassword = desUtil.decrypt(userPassword);
      }
      catch (Exception e) {
        logger.error("password decrypt error!");
      }

    }

    cognosClient = CognosHelper.getClient();
    cognosClient.setDispatch(cognos_URL);
    cognosClient.setUsername(userName);
    cognosClient.setPassword(userPassword);
    cognosClient.setNamespace(userNamespace);
    try {
      cognosClient.logon();
    }
    catch (LoginException e) {
      e.printStackTrace();
      logger.error("Connect to Cognos failed, please check username & pasword in config.xml or Cognos enviroment.");
      return null;
    }
    return cognosClient; }

  public String updateMacro(String macro, String hash) {
    StringBuffer sb = new StringBuffer();
    String[] conds = macro.split("or");
    boolean appended = false;
    String amdstr = "#sq($" + 
      hash + 
      "{prompt(&#39;MUDID&#39;, &#39;token&#39;, &#39;&#39;&#39;&#39;&#39;&#39;)})# = &#39;admin&#39;";
    for (int i = 0; i < conds.length; ++i) {
      String cond = conds[i].trim();
      if (!cond.startsWith("#")) {
        sb.append(cond.replaceAll("upper", "lower").replaceAll("UPPER", 
          "LOWER"));
        sb.append(" or \n");
      }
      else
      {
        sb.append(amdstr);
        appended = true;
      }
    }
    if (!appended) {
      sb.append(amdstr);
    }
    return sb.toString();
  }
  public String updateSpec(String spec, String hash) {
    if (hash == null) hash = "DataAdmin";
    String keyword = "MUDID";
    int p = spec.indexOf("?" + keyword + "?");
    String con0 = null;
    String con1 = null;
    String con2 = null;
    String macro = null;
    if (p > 0) {
      int lp = spec.substring(0, p).lastIndexOf(">");
      int rp = spec.substring(p).indexOf("<");
      if ((lp > 0) && (rp > 0)) {
        macro = spec.substring(lp + 1, rp + p);
        con0 = spec.substring(0, lp + 1);
        con1 = spec.substring(rp + p);
        if (con1.indexOf("?" + keyword + "?") > 0)
          con2 = updateSpec(con1, hash);
        else {
          con2 = con1;
        }
      }
    }
    if ((macro != null) && 
      (con0 != null) && (con2 != null)) {
      return con0 + updateMacro(macro, hash) + con2;
    }

    return spec;
  }
}